<?php
/**
 * Created by PhpStorm.
 * User: wo
 * Date: 2019/8/27
 * Time: 19:48
 */
class ShareApp extends MallbaseApp{

    //分享注册新用户
    function index(){
        $this->display('share.index.html');
    }

    /**
     * @api {get}  注册相关接口
     */
    function shareRegister() {
        header("Content-type: text/html; charset=utf-8");
        $user_info = array();
        $phone_mob = $_POST['phone'];
        $msg_sign = $_POST['msg_sign'];
        $password = $_POST['password'];
        $confirm_password = $_POST['confirm_password'];
        $msg_code = $_POST['code'];
        $city = $_POST['city'];
        $token = $_POST['token']; //推荐人id
        $apptoken = &m('app_token');
        $referid = $apptoken->get(array('conditions'=>"token = '$token'"));
        try{
           $param = array(
               'phone_mob' => $phone_mob,
               'msg_sign'  => $msg_sign,
               'password'  => $password,
               'confirm_password' => $confirm_password,
               'msg_code'  => $msg_code,
               'city'      => $city,
               'referid'   => $referid,
               'refer_type'=>1
           );
           $arr = $this->method($param);
        }catch(\Exception $e){
            $arr['code'] = -1;
            $arr['msg'] = '网络错误';
        }
        echo json_encode($arr);
        exit();
    }
    function method($param){
        $phone_mob = $param['param'];
        $msg_code = $param['msg_code'];
        $confirm_password = $param['confirm_password'];
        $password = $param['password'];
        $msg_sign = $param['msg_sign'];
        $city = $param['city'];
        $referid = $param['referid'];

        $arr['code'] = -1;
        $arr['msg'] = '缺少参数';
        if($phone_mob &&  $msg_code && $confirm_password && $password){
            $arr['msg'] = '短信验证码错误';
            if (md5('HomeIndexSet_password' . $msg_code) == $msg_sign) {
                $arr['msg'] = '注册失败，用户名长度为3-25之间！';
                $user_name = $phone_mob;
                $user_name_len = strlen($user_name);
                if ($user_name_len >= 3 && $user_name_len <= 25) {
                    $arr['msg'] = '输入两次密码不一致';
                    if ($password == $confirm_password) {
                        $arr['msg'] = '密码长度必须大于6位';
                        $passlen = strlen($password);
                        if ($passlen >= 6 && $passlen <= 20) {
                            //实例化
                            $region = &m('region');
                            $member = &m("member");
                            //验证手机号是否已存在
                            $memberInfo = $member->check_mobile($phone_mob);
                            $arr['msg'] = $memberInfo['msg'];
                            if($memberInfo['code'] == 1){

                                $store = &m("store");
                                $integrallog = &m('integral_log');
                                $appToken = &m('app_token');

                                //城市
                                $res_city = $region->get(array('conditions' => 'region_name like "%'.$city.'%"'));
                                $res_region = $region->get(array('conditions'=>'region_id in ('.$res_city['region_id'].','.$res_city['parent_id'].')'));
                                $res_store = $store->get(array('conditions'=>'is_main = 0 AND region_id = '.$res_region['region_id']));
                                if(!$res_store){
                                    $res_store = $store->get(array('conditions'=>'is_main = 1 AND region_id = '.$res_region['region_id']));

                                    if(!$res_store){
                                        echo json_encode(array('code'=>-1,'msg'=>'查找仓库失败'));
                                        exit();
                                    }
                                }

                                if($res_store){
                                    $user_id = $member->share_register($user_name, $password, '', array('phone_mob' => $phone_mob, 's_id'=>$res_store['s_id'],'s_name'=>$res_store['s_name'],'referid'=>$referid['user_id'],'refer_type'=>$param['refer_type']));
                                    $arr['msg'] = $user_id['msg'];
                                    if($user_id['code'] == 1){
                                        $token = session_id();
                                        //保存到app_token表
                                        $appToken->add(array(
                                            'user_id' => $user_id['result'],
                                            'token' => $token
                                        ));
                                        $arr['code'] = 1;
                                        $arr['msg'] = '注册成功';
                                        $arr['user_id'] = $user_id['result'];
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return $arr;
    }

    /**
     * @api {get} 发送短信接口
     */
    function send_messages() {
        $to_mobile = $_POST['mobile'];
        if (!$to_mobile) {
            echo json_encode(array(
                'code' => -1,
                'message' => '手机号码为空',
            ));
            exit;
        }
        $member = &m('member');
        $memberInfo = $member->check_mobile($to_mobile);
        if($memberInfo['code'] == -1){
            echo json_encode(array(
                'code' => $memberInfo['code'],
                'message' => $memberInfo['msg'],
            ));
            exit;
        }
        $msgLog = &m('msglog');
        $result = $msgLog->send_msg_system('register', $to_mobile);
        $msg_sign = md5('HomeIndexSet_password' . $_SESSION['MobileConfirmCode']);
        echo json_encode(array(
            'code' => $result['code'],
            'message' => $result['msg'],
            'result' => array('msg_sign' => $msg_sign)
        ));
        exit;
    }


    /** 心愿单分享用户*/
    function shareWish(){
        $this->display('share.wish.html');
    }

    /** 通过分享心愿单注册 */
    function shareWishRegister(){
        header("Content-type: text/html; charset=utf-8");
        $user_info = array();
        $phone_mob = $_POST['phone'];
        $msg_sign = $_POST['msg_sign'];
        $password = $_POST['password'];
        $confirm_password = $_POST['confirm_password'];
        $msg_code = $_POST['code'];
        $city = $_POST['city'];
        $token = $_POST['token']; //分享人id
        $wish_id = $_POST['wish_id'];
        if(!$phone_mob || !$msg_sign || !$password || !$confirm_password || !$msg_code || !$city || !$token || !$wish_id){
            echo json_encode(array('code'=> -1,'msg'=>'参数错误'));
            exit();
        }
        $apptoken = &m('app_token');
        $referid = $apptoken->get(array('conditions'=>"token = '$token'"));
        try{
            $param = array(
                'phone_mob' => $phone_mob,
                'msg_sign'  => $msg_sign,
                'password'  => $password,
                'confirm_password' => $confirm_password,
                'msg_code'  => $msg_code,
                'city'      => $city,
                'referid'   => $referid,
                'refer_type'=> 2
            );
            $result = $this->method($param);
            //注册成功后进行砍单
            if($result['code'] == 1){

                //查询当前心愿单状况
                $wish = &m('wish');
                $wishPaylog = &m("wish_pay_log");
                $wishlog = &m('wish_log');

                $wishInfo = $wish->get(array('conditions'=>array('id'=>$wish_id)));
                $arr['msg'] = '心愿单状态不符合当前操作';
                if($wishInfo['state'] == 1){
                    //查询当前已支付金额 以及帮砍金额
                    $wishpaylogMoney = $wishPaylog->getPayLog($wish_id,1);
                    $wishlogMoney = $wishlog->getInvalid($wish_id);
                    //期望价格 和 已付金额+帮砍金额 比较
                    $total_price = $wishpaylogMoney + $wishlogMoney;
                    if($wishInfo['wish_price'] > $total_price){
                        $insertData = array(
                            'user_id' => $referid['user_id'],//发起人
                            'inviter_id' => $arr['user_id'],//帮砍人id
                            'wish_id' => $wish_id,          //心愿单id
                            'money'   => 60,                //固定金额
                            'msg'     => '帮砍金额',
                            'create_time'=>time()
                        );
                        $insert = $wishlog->add($insertData);
                        //再次查询当前金额和期望金额比较
                        $newWishlogMoney = $wishlog->getInvalid($wish_id);
                        $newTotalMoney = $wishpaylogMoney + $newWishlogMoney;
                        if($wishInfo['wish_price'] <= $newTotalMoney){
                            //调取生成订单接口
                            $info = $wish->createOrder($wish_id);
                            $arr['msg'] = $info['msg'];
                            if($info['code'] == 1){
                                $arr['code'] = 1;
                            }
                        }else{
                            if($insert){
                                $arr['code'] = 1;
                                $arr['msg'] = 'success';
                            }
                        }
                    }else{
                        //调取生成订单接口
                        $info = $wish->createOrder($wish_id);
                        $arr['msg'] = $info['msg'];
                        if($info['code'] == 1){
                            $arr['code'] = 1;
                        }
                    }
                }
            }
        }catch(\Exception $e){
            $arr['code'] = -1;
            $arr['msg'] = '网络错误';
        }
        echo json_encode($arr);
        exit();
    }

}